home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 18 / develop 18 code / Graphics Speed on PwrPC / BlitCmp.c < prev    next >
Encoding:
Text File  |  1994-03-08  |  2.6 KB  |  94 lines  |  [TEXT/MMCC]

  1. //#include <Types.h>
  2. //#include <Traps.h>
  3. //#include <QuickDraw.h>
  4. //#include <FixMath.h>
  5. #include <QDOffscreen.h>
  6. #include <OSUtils.h>
  7. #include <Timer.h>
  8.  
  9. #include "BlitCmp.h"            // Prototype declarations
  10.  
  11. // Decide for yourself how many microseconds represent a "meaningful"
  12. // difference.
  13. #define        kMeaningfulDiff        0
  14. #define        ABS(x)            ((x < 0)? (-x) : (x))
  15.  
  16. #if powerc
  17.     extern QDGlobals qd;
  18. #else    // We have to implement our own WideSubtract() for 68K
  19. wide * WideSubtract(wide *target, const wide *source)
  20. {
  21.     target->hi -= source->hi;
  22.     if (target->lo < source->lo)
  23.         target->hi--;
  24.     target->lo -= source->lo;
  25.     
  26.     return target;
  27. }
  28. #endif
  29.  
  30. unsigned long TimeBlitProc(BlitProcPtr theBlitProc, 
  31.     BitMapPtr srcBits, BitMapPtr dstBits, 
  32.     Rect *srcRect, Rect *dstRect,
  33.     short mode, RgnHandle mask)
  34. {
  35.     UnsignedWide    startMicroSec, endMicroSec;
  36.  
  37.     Microseconds(&startMicroSec);
  38.  
  39.     (*theBlitProc)(srcBits, dstBits, srcRect, dstRect, mode, mask);
  40.  
  41.     Microseconds(&endMicroSec);
  42.     WideSubtract((wide *) &endMicroSec, (wide *) &startMicroSec);
  43.     return endMicroSec.lo;
  44. }
  45.  
  46. BlitProcPtr    BestBlitter(BlitProcPtr customBlitProc, 
  47.     PixMapHandle srcPixHandle, Rect *srcRect, Rect *dstRect)
  48. {
  49.     unsigned long    customBitsTime, copyBitsTime;
  50.     long                leDifference;
  51.     PixMapHandle    portPixMap;
  52.     BlitProcPtr        copyBitsPtr;
  53.     Str255            numStr;
  54.     
  55.     // To factor out the trap overhead, get the trap address
  56.     // for CopyBits. Power PC can get the address of the shared
  57.     // library routine directly.
  58.     // By getting the address of the library routine like this
  59.     // we don't need to worry about calling CopyBits through
  60.     // CallUniversalProc
  61. #if powerc
  62.     copyBitsPtr = (BlitProcPtr) &CopyBits;
  63. #else
  64.     copyBitsPtr = (BlitProcPtr) GetToolTrapAddress(_CopyBits);
  65. #endif
  66.      
  67.      portPixMap = ((CGrafPtr)qd.thePort)->portPixMap;
  68.  
  69.     // Normally, it's not necessary to lock a pixMap or its pixels
  70.     // before calling CopyBits. But in this case, we're calling
  71.     // TimeBlitProc, which could hit the segment loader and cause
  72.     // memory to move. So we lock the pixMap handles before
  73.     // dereferencing them here.
  74.      HLock((Handle) portPixMap);
  75.      LockPixels(portPixMap);
  76.             
  77.     copyBitsTime = TimeBlitProc(copyBitsPtr, 
  78.         (BitMapPtr) (*srcPixHandle), (BitMapPtr) (*portPixMap),
  79.         srcRect, dstRect, srcCopy, nil);
  80.     customBitsTime = TimeBlitProc(customBlitProc,
  81.         (BitMapPtr) (*srcPixHandle), (BitMapPtr) (*portPixMap),
  82.          srcRect, dstRect, srcCopy, nil);                
  83.     
  84.     UnlockPixels(portPixMap);
  85.     HUnlock((Handle) portPixMap);
  86.  
  87.     leDifference = (long) (customBitsTime - copyBitsTime);
  88.     if (ABS(leDifference) > kMeaningfulDiff && leDifference < 0)
  89.         return customBlitProc;
  90.     else
  91.         return copyBitsPtr;
  92. }
  93.  
  94.